package com.dk.base.web.api;


import com.dk.base.core.service.DemoService;
import com.dk.base.web.mapper.DemoMapper;
import com.dk.base.web.vo.DemoVO;
import com.dk.common.defs.CommonDefs;
import com.dk.common.exception.BusinessException;
import com.dk.common.result.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Tag(name = "demo接口")
@RestController
@RequestMapping(CommonDefs.RESTFUL_URI_CONTEXT + "/demo")
public class DemoApi {


    @Autowired
    private DemoService demoService;
    @Autowired
    private DemoMapper demoMapper;

    @Operation(summary = "查询列表")
    @GetMapping("/list")
    public ApiResult<List<DemoVO>> list() {
        return ApiResult.success(demoService.list().stream().map(dto -> demoMapper.convert(dto)).toList());
    }

    @Operation(summary = "插入数据")
    @GetMapping("/insert/{num}")
    public Integer insert(@PathVariable Integer num) {
        if (num == 0) {
            throw new BusinessException("num can not 0");
        }
        return demoService.insert(num);

    }

}
